home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / OLECLI.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  8KB  |  317 lines

  1. /*
  2.  * OLECLI.C
  3.  *
  4.  * Constructor, Destructor, and the single method "CallBack" (called
  5.  * ClientCallback here) for the CLIENT structure.  The OLECLI DLL calls
  6.  * ClientCallback through the OLECLIENTVTBL stored with this structure.
  7.  *
  8.  * The CLIENT structure contains a pointer to the application-wide
  9.  * OLESTREAM structure.
  10.  *
  11.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  12.  */
  13.  
  14. #include <windows.h>
  15. #include <ole.h>
  16. #include "oclient.h"
  17.  
  18.  
  19.  
  20.  
  21. //Array of string pointers, global to OLE-specific code.
  22. char NEAR *rgpszOLE[COLESTRINGS];
  23.  
  24.  
  25.  
  26.  
  27. /*
  28.  * PClientAllocate
  29.  *
  30.  * Purpose:
  31.  *  Constructor method for the CLIENT data type.  Allocates a CLIENT
  32.  *  and sets the defaults in its fields:
  33.  *      Initalize OLECLIENTVTBL and OLESTREAMVTBL
  34.  *      Allocate and initialize an OLESTREAM structure (see OLESTREA.C)
  35.  *      Register OLE clipboard formats
  36.  *      Allocate scratch data and set pointers within it.
  37.  *
  38.  *  Used from application initialization.
  39.  *
  40.  * Parameters:
  41.  *  pfSuccess       LPBOOL indicating if the initialization succeeded.  If
  42.  *                  this function returns non-NULL, but *pfSuccess==FALSE,
  43.  *                  the caller must call the destructor function.
  44.  *  hInst           HANDLE of the application instance.
  45.  *  pfnCallBack     FARPROC to the single client method to initialize.
  46.  *                  We pass this function to PVtblClientAllocate.
  47.  *
  48.  * Return Value:
  49.  *  LPCLIENT        Pointer to the allocated CLIENT if successful,  NULL
  50.  *                  if the allocation failed or a parameter is invalid.
  51.  */
  52.  
  53. LPCLIENT FAR PASCAL PClientAllocate(LPBOOL pfSuccess, HANDLE hInst,
  54.                                     LPCLIENTCALLBACK pfnCallBack)
  55.     {
  56.     LPCLIENT            pCli;
  57.     HANDLE              hMem;
  58.     BOOL                fTemp;
  59.  
  60.     /*
  61.      * Any error condition will return FALSE unless we get all through
  62.      * this function.  This scheme allows us to always return on any
  63.      * error instead of trying to clean up what was already initialized.
  64.      * Instead, we let the destructor, PClientFree, do the work.
  65.      */
  66.  
  67.     if (NULL==pfSuccess)
  68.         return NULL;
  69.  
  70.     *pfSuccess=FALSE;
  71.  
  72.     if (NULL==hInst)
  73.         return NULL;
  74.  
  75.     //Allocate this structure.
  76.     hMem=LocalAlloc(LPTR, CBCLIENT);
  77.  
  78.     if (NULL==hMem)
  79.         return NULL;
  80.  
  81.     pCli=(LPCLIENT)(PSTR)hMem;
  82.  
  83.     //Go load the strings for OLE before we start using them
  84.     pCli->hMemStrings=HLoadOLEStrings(hInst);
  85.  
  86.     if (NULL==pCli->hMemStrings)
  87.         return pCli;
  88.  
  89.  
  90.     //1. Register clipboard formats; the strings are globals in this file.
  91.     pCli->cfNative    =RegisterClipboardFormat(PSZOLE(IDS_NATIVE));
  92.     pCli->cfOwnerLink =RegisterClipboardFormat(PSZOLE(IDS_OWNERLINK));
  93.     pCli->cfObjectLink=RegisterClipboardFormat(PSZOLE(IDS_OBJECTLINK));
  94.  
  95.     //Any error, return what we already allocated.
  96.     if (0==pCli->cfNative || 0==pCli->cfOwnerLink || 0==pCli->cfObjectLink)
  97.         return pCli;
  98.  
  99.  
  100.     //2. Get initialized OLECLIENTVTBL pointer.
  101.     pCli->pvt=PVtblClientAllocate(&fTemp, hInst, pfnCallBack);
  102.  
  103.     if (!fTemp)
  104.         return pCli;
  105.  
  106.  
  107.     //3. Get an initialized STREAM pointer.
  108.     pCli->pStream=PStreamAllocate(&fTemp, hInst);
  109.  
  110.     if (!fTemp)
  111.         return pCli;
  112.  
  113.  
  114.     //4.  Allocate scratch memory.
  115.     pCli->hData=GlobalAlloc(GHND, CSCRATCH*CBSCRATCH);
  116.  
  117.     if (NULL==pCli->hData)
  118.         return pCli;
  119.  
  120.     /*
  121.      * Initialize global pointers into this memory.  Since we know we
  122.      * allocated with a nonzero byte count, GlobalLock will work.
  123.      */
  124.     pCli->pszData1=GlobalLock(pCli->hData);
  125.     pCli->pszData2=pCli->pszData1+CBSCRATCH;
  126.     pCli->pszData3=pCli->pszData2+CBSCRATCH;
  127.  
  128.  
  129.     //Everything handled successfully.
  130.     *pfSuccess=TRUE;
  131.     return pCli;
  132.     }
  133.  
  134.  
  135.  
  136.  
  137. /*
  138.  * PClientFree
  139.  *
  140.  * Purpose:
  141.  *  Frees all data in the CLIENT and frees the structure.
  142.  *
  143.  * Parameters:
  144.  *  pCli            LPCLIENT to the structure to free.
  145.  *
  146.  * Return Value:
  147.  *  LPCLIENT        NULL if the function succeeds, pCli otherwise.
  148.  */
  149.  
  150. LPCLIENT FAR PASCAL PClientFree(LPCLIENT pCli)
  151.     {
  152.     BOOL        fRet=FALSE;
  153.  
  154.     /*
  155.      * Free the scratch memory if we have any.  No need to clear the
  156.      * pointers since we'll be freeing this structure anyway.
  157.      */
  158.  
  159.     if (NULL!=pCli->hData)
  160.         GlobalFree(pCli->hData);
  161.  
  162.     //Free the stream we're holding
  163.     if (NULL!=PStreamFree(pCli->pStream))
  164.         return pCli;
  165.  
  166.     //Free this object's VTBL
  167.     if (NULL!=PVtblClientFree(pCli->pvt))
  168.         return pCli;
  169.  
  170.     //Free the strings
  171.     if (NULL!=pCli->hMemStrings)
  172.         LocalFree(pCli->hMemStrings);
  173.  
  174.  
  175.     if (NULL!=LocalFree((HANDLE)(DWORD)pCli))
  176.         return pCli;
  177.  
  178.     return NULL;
  179.     }
  180.  
  181.  
  182.  
  183.  
  184. /*
  185.  * PClientWindowSet
  186.  *
  187.  * Purpose:
  188.  *  Informs the CLIENT structure about the main application window
  189.  *  which it needs to post messages from ClientCallback.
  190.  *
  191.  * Parameters:
  192.  *  pCli            LPCLIENT to the structure concerned.
  193.  *  hWnd            HWND of the application window.
  194.  *
  195.  * Return Value:
  196.  *  None
  197.  */
  198.  
  199. void FAR PASCAL PClientWindowSet(LPCLIENT pCli, HWND hWnd)
  200.     {
  201.     if (NULL!=pCli)
  202.         pCli->hWnd=hWnd;
  203.  
  204.     return;
  205.     }
  206.  
  207.  
  208.  
  209.  
  210.  
  211. /*
  212.  * PClientMsgProcSet
  213.  *
  214.  * Purpose:
  215.  *  Informs the CLIENT structure about a function in the main application
  216.  *  that translates and dispatches messages.  This prevents the CLIENT
  217.  *  from having to carry an accelerator handle or window handle and
  218.  *  allows the application to perform other actions we cannot predict
  219.  *  (like IsDialogMessage).
  220.  *
  221.  * Parameters:
  222.  *  pCli            LPCLIENT to the structure concerned.
  223.  *  pfn             LPFNMSGPROC to the message processing function.
  224.  *
  225.  * Return Value:
  226.  *  None
  227.  */
  228.  
  229. void FAR PASCAL PClientMsgProcSet(LPCLIENT pCli, LPFNMSGPROC pfn)
  230.     {
  231.     if (NULL!=pCli)
  232.         pCli->pfnMsgProc=pfn;
  233.  
  234.     return;
  235.     }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. /*
  244.  * HLoadOLEStrings
  245.  *
  246.  * Purpose:
  247.  *  Allocates FIXED local memory and reads the applications
  248.  *  string resources into that memory.  Each string's pointer
  249.  *  is available through the PSZOLE(i) macro where i is the ID
  250.  *  value of the string.  The strings must have sequential IDs.
  251.  *
  252.  *  Note that string pointers are stored in the rgpszOLE global
  253.  *  array defined in this file.
  254.  *
  255.  * Parameters:
  256.  *  hInst           HANDLE of the application instance.
  257.  *
  258.  * Return Value:
  259.  *  HANDLE          Handle to the local memory.  NULL if memory could
  260.  *                  not be allocated.
  261.  */
  262.  
  263. HANDLE PASCAL HLoadOLEStrings(HANDLE hInst)
  264.     {
  265.     HANDLE      hMem;
  266.     char NEAR   *pch;
  267.     WORD        cchUsed=0;
  268.     WORD        cch;
  269.     short       i;
  270.  
  271.     /*
  272.      * Allocate memory and load strings.  NOTE!  The LPTR style
  273.      * specifies FIXED memory.  This should not be a big deal
  274.      * since this is an early allocation into the local heap.
  275.      * But it should be watched if the number of strings becomes
  276.      * large.
  277.      */
  278.     hMem=LocalAlloc(LPTR, COLESTRINGS*CCHOLESTRINGMAX);
  279.  
  280.     if (hMem==NULL)
  281.         return (HANDLE)NULL;
  282.  
  283.     /*
  284.      * This operation is only valid for FIXED memory.  Otherwise use
  285.      * LocalLock.
  286.      */
  287.     pch=(char *)hMem;
  288.  
  289.  
  290.     /*
  291.      * Load the strings into the memory and retain the specific
  292.      * pointer to that string.
  293.      */
  294.     for (i=0; i < COLESTRINGS; i++)
  295.         {
  296.         cch=LoadString(hInst, i, (LPSTR)(pch+cchUsed), CCHOLESTRINGMAX-1);
  297.         PSZOLE(i)=(char *)(pch+cchUsed);
  298.  
  299.         /*
  300.          * One is added to cch to include a NULL.  The memory was ZEROINITed
  301.          * on allocation so by skipping a byte we get the NULL.
  302.          */
  303.         cchUsed +=cch+1;
  304.         }
  305.  
  306.     /*
  307.      * We are assuming that no string is over CCHSTRINGMAX, and therefore
  308.      * we did not use all the allocated memory.  Therefore LocalReAlloc
  309.      * will only SHRINK the block, never expand it.  So if it fails, we
  310.      * don't care--all the strings are still there, we just wasted some
  311.      * space.
  312.      */
  313.     LocalReAlloc(hMem, cchUsed+1, LPTR);
  314.  
  315.     return hMem;
  316.     }
  317.